<?php
namespace app\www\controller;
use x\Xbase;
use x\my\pay\Alipay;
class Notify extends Xbase{

    //支付宝网页支付充值
    public function alipayRecharge(){
        $pay = new Alipay();
        $notify = $this->post;
        //验证
        if ($pay->verifyNotify($notify)) {
            //获取订单信息
            $info = $pay->getInfo();
            if ($info['status']) {
                //成功,处理
                $order_no = $info["out_trade_no"];
                //修改订单状态
                $rs = $this->db->query('select * from recharge where order_no=?',[$order_no],1);
                $this->db->begin();
                $mysql = [];
                #更新订单
                $d = [
                    'id'=>$rs['id'],
                    'pay_type'=>1,
                    'pay_status'=>1,
                    'pay_order_no'=>'',
                    'update_time'=>time()
                ];
                $mysql[] = $this->db->update("recharge",$d);
                #插入账单
                $mysql[] = m('Userbill', 'xcom')->doit($order_no, $rs['user_id'], 100, 'recharge', $rs['id'],  $rs['real_money'],'money', '充值',$rs['fee']);
                $rs = $this->db->end($mysql);
                if(!$rs){
                    err('支付账单失败');
                }
                $pay->notifySuccess();
            } else {
                err("支付失败！");
            }
        } else {
            exit("Access Denied");
        }
    }

    //异步通知微信充值
    public function wxRecharge(){
        $app = m('weixin','xcom')->getPayApp();
        log_test('wxpay_to');
        $response = $app->handlePaidNotify(function($message, $fail){
            $rs = $this->db->query('select * from recharge where order_no=?',[$message['out_trade_no']],1);
            if (!$rs) {
                exit($fail('Order not exist.'));
            }elseif($rs['pay_status']!=0){
                err('已支付');
            }
            log_test('wxpay_result',$message);
            if($message['result_code'] == 'SUCCESS'){
                $pay_order_no = $message['transaction_id'];
                $this->db->begin();
                $mysql = [];
                #更新订单
                $d = [
                    'id'=>$rs['id'],
                    'pay_type'=>0,
                    'pay_status'=>1,
                    'pay_order_no'=>$pay_order_no,
                    'update_time'=>time()
                ];
                $mysql[] = $this->db->update($d);
                #插入账单
                $mysql[] = m('Userbill', 'xcom')->doit($message['out_trade_no'], $rs['user_id'], 100, 'recharge', $rs['id'],  $rs['money'],'money', '充值',$rs['fee']);
                $rs = $this->db->end($mysql);
                if(!$rs){
                    log_test('wxpay_result_db',$mysql);
                    err('支付账单失败');
                }
            }else{
                err('支付错误');
            }
        });
        exit($response);
    }

    //获取订单编号
    public function getRecharge(){
        $order_no = $this->post('order_no');
        $rs = $this->db->query('select id from recharge where order_no=?',[$order_no],1);
        if($rs){
            suc();
        }else{
            err();
        }
    }
}
